import {
  isNavBarColumn,
  isRawRecord
} from "@src/lib/decorators/conditions/interface.condition";

export const hasArrayLength = (
  length: number
): ((parameter: any) => boolean) => {
  return (parameter: any): boolean => {
    if (!(parameter instanceof Array)) {
      return false;
    }
    if (parameter.length !== length) {
      return false;
    }

    return true;
  };
};

export const isArray = (
  arrayType: string,
  allowEmpty = true,
  condition: ((element: any) => boolean) | undefined = undefined
): ((parameter: any) => boolean) => {
  return (parameter: any): boolean => {
    if (!(parameter instanceof Array)) {
      return false;
    }
    if (!allowEmpty && parameter.length === 0) {
      return false;
    }

    for (const e of parameter) {
      if (typeof e !== arrayType) {
        return false;
      }
      if (condition !== undefined && !condition(e)) {
        return false;
      }
    }

    return true;
  };
};

export const isInterfaceArray = (
  isInterfaceOf: (parameter: any) => boolean,
  allowEmpty = true,
  condition: ((element: any) => boolean) | undefined = undefined
): ((parameter: any) => boolean) => {
  return (parameter: any): boolean => {
    if (!(parameter instanceof Array)) {
      return false;
    }
    if (!allowEmpty && parameter.length === 0) {
      return false;
    }

    for (const e of parameter) {
      if (!isInterfaceOf(e)) {
        return false;
      }
      if (condition !== undefined && !condition(e)) {
        return false;
      }
    }

    return true;
  };
};

export const isNavBarArray = isInterfaceArray(isNavBarColumn, false);

export const isSimpleArray = isArray(
  "string",
  true,
  (element: any): boolean => {
    return element.indexOf(",") === -1;
  }
);

export const isRawRecordArray = isInterfaceArray(isRawRecord, false);
